<?php

include_once 'database.php';
include_once 'supervisor.php';

class Subject {
    
    private $sub_code;
    private $sub_name;
    private $sub_stdcount;
    private $sub_day;
    private $sub_start;
    private $sub_end;
    private $sub_available;
    
    private $_a_subject_object;
    
    public function getCode(){
        return $this->sub_code;
    }
    
    public function getName() {
        return $this->sub_name;
    }
    
    public function getStdCount() {
        return $this->sub_stdcount;
    }
    
    public function getDay() {
        return $this->sub_day;
    }
    
    public function getStart() {
        return $this->sub_start;
    }
    
    public function getEnd() {
        return $this->sub_end;
    }
    
    public function getAvailable() {
        return $this->sub_available;
    }
    
    public function __construct($code, $name, $stdcount, $day, $start, $end, $available) {
        $this->sub_code = $code;
        $this->sub_name = $name;
        $this->sub_stdcount = $stdcount;
        $this->sub_day = $day;
        $this->sub_start = $start;
        $this->sub_end = $end;
        $this->sub_available = $available;
    }
    
    public static function addSubject($code, $name, $stdcount, $day, $start, $end, $available, $supervisors = NULL, $hall = NULL) {
        
        $dbo = Database::getDBOInstance();
        
        //echo "insert into sase_subject values ('$code', '$name', $stdcount, '$day', '$start', '$end', $available)<br>";
        $dbo->mysql_query("delete from sase_subject where sub_code = '$code'");
        $dbo->mysql_query("insert into sase_subject values ('$code', '$name', $stdcount, '$day', '$start', '$end', $available, '$hall')");
        $dbo->mysql_query("delete from subject_sup_count where sub_code = '$code'"); 
        $dbo->mysql_query("insert into subject_sup_count values ('$code', 0)");
        
        if (isset($supervisors)) {
            foreach ($supervisors as $sup_id) {
                //echo 'adding def';
                Supervisor::enrolDefaultSupervisor($code, $sup_id);    
            }    
        }

    }
    
    public static function getSubjectObject($code) {
        
        if (!isset ($this->_a_subject_object)) {
            $dbo = Database::getDBOInstance();
            $result = $dbo ->mysql_query("select * from sase_subject where sub_code = '$code'");
            $row = mysql_fetch_assoc($result);

            $this->_a_subject_object = new Subject($row['sub_code'],$row['sub_name'],$row['sub_stdcount'],$row['sub_day'],$row['sud_start'],$row['sud_end'], $row['sub_available']);
        }
            
        return $this->_a_subject_object;
    }
    
    
    public static function getSubjectDays() {
        $dbo = Database::getDBOInstance();
        $result = $dbo->mysql_query("select distinct sub_day from sase_subject order by sub_day");
        
        $day_list = array();
        
        while ($row = mysql_fetch_assoc($result)) {
            $day_list[] = $row['sub_day'];
        }
        
        return $day_list;
    }
    
    public static function getTimeList($day = NULL) {
        $dbo = Database::getDBOInstance();
        
        if (isset($day)) {
            $result = $dbo->mysql_query("select sub_start, sub_end from sase_subject where sub_day = '$day' order by sub_start, sub_end");    
        }
        
        $time_solt = array();
        while ($row = mysql_fetch_assoc($result)) {
            $time_solt[] = array('sub_start' => $row['sub_start'], 'sub_end' => $row['sub_end']);
        }
        
        return $time_solt;
    }
    
    public static function getRemainderDefaultSupervisors($subjectID) {
        $dbo = Database::getDBOInstance();
        
        //Check for how many of the default supervises are still supervising the subject
    	$query = "select distinct ss.sup_id from default_supervisor as ds, supervises_subject as ss where ds.sub_code = ss.sub_code and ss.sub_code = '$subjectID'";

    	$result = $dbo->mysql_query($query);
        
        return mysql_num_rows($result);
    }
    
    public static function getUnallocatedSubjects() {
        $dbo = Database::getDBOInstance();
        
        //Get the ratio between supervisors and a subject 
        $query = "select * from general where property = 'std_sup_ratio'";
        $result = $dbo->mysql_query($query);
        $row = mysql_fetch_assoc($result);
        $ratio = $row['value'];
        
        //We need to join 2 tables to get the desired output, the sase_subject which has stdcount and subject_sup_count which has supervisor counts
        $query = "select supcount.sub_code, sub.sub_stdcount, supcount.sub_count "
            . "from sase_subject as sub, subject_sup_count as supcount " 
            . "where " 
            . "supcount.sub_code = sub.sub_code and "
            . "sub.sub_available = 1 and "
            . "( sub.sub_stdcount / supcount.sub_count ) > $ratio";
        
        $result = $dbo->mysql_query($query);
        
        $list = array();
        
        while ($row = mysql_fetch_assoc($result)) {
            $list[] = $row['sub_code'];
        }
        
        $query = "select * from subject_sup_count where sub_count = 0";
        $result = $dbo->mysql_query($query);
        
        while ($row = mysql_fetch_assoc($result)) {
            $list[] = $row['sub_code'];
        }
        
        sort($list);
        
        return $list;
    }
    
    public static function getAllocatedSubjects() {
        
    }
    
    public static function getTotalSubjects(){
        $dbo = Database::getDBOInstance();
        
        $query = "select * from sase_subject";
        $result = $dbo->mysql_query($query);
        
        return mysql_num_rows($result);
    }
    
    /**
     * This will provide an array of subject codes that are defined in supervises_subject / 
     * default supervisor but not in sase_subject
     */ 
    public static function getInconsistancy() {
        $dbo = Database::getDBOInstance();
        
        $ic_subs = array();
        $ss_subs = array(); //SASE_Subject sub codes
        $ds_subs = array(); //Hold subcodes from default supervisor
        $se_subs = array(); //hold subcodes from supervises exam
        
        $result = $dbo->mysql_query("select sub_code from sase_subject");
        
        while ($row = mysql_fetch_assoc($result)) {
            $ss_subs[] = $row['sub_code'];
        }
        
        
        $result = $dbo->mysql_query("select sub_code from default_supervisor");
        
        while ($row = mysql_fetch_assoc($result)) {
            $ds_subs[] = $row['sub_code'];
        }
        
        $result = $dbo->mysql_query("select sub_code from supervises_subject");
        
        while ($row = mysql_fetch_assoc($result)) {
            $se_subs[] = $row['sub_code'];
        }
        
        //Search default supervisors that are not in the sase subject
        foreach ($ds_subs as $sub) {
            if ( array_search($sub, $ss_subs) === FALSE )
                $ic_subs[] = $sub;
        }
        
        
        //Search supervises_subject that are not in the sase_subject
        foreach ($se_subs as $sub) {
            if ( ( array_search($sub, $ss_subs) ) && ( array_search($sub, $ic_subs) ) )
                $ic_subs[] = $sub;
        }
        
        $unique = array_unique($ic_subs);
        sort($unique);
        
        return $unique;
    }
    
    /**
     * Will delete a entry from the database and handle related activity with relations 
     * 
     * @param String $subCode The subject code
     */
    public static function deleteSubject($subCode) {
        $dbo = Database::getDBOInstance();
        
        //Delete from counters
        $dbo->mysql_query("delete from subject_sup_count where sub_code = '$subCode'");
        
        //Get the list who are allocated to the subject
        $list = Subject::getAllocatedSupervisors($subCode);
        
        //Remove them from allocation
        foreach ($list as $sup) {
            Supervisor::dropSubject($sup, $subCode);
        }
        
        //remove the subject
        $dbo->mysql_query("delete from sase_subject where sub_code = '$subCode'");
        
        //remove the default_supervisors
        $dbo->mysql_query("delete from default_supervisor where sub_code = '$subCode'");
    }
    
    
    public static function dropDefaultSubject($subCode) {
        
    }
    
    
    public static function getAllocatedSupervisors($subCode) {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from supervises_subject where sub_code = '$subCode'");
        
        $list = array();
        while ($row = mysql_fetch_assoc($result)) {
            $list[] = $row['sup_id'];
        }
        $list = array_unique($list);
        sort($list);
        
        return $list;
    }
    
    public static function updateSubject($subCode, $subDetails, $subDefaultSupervisors) {
        $sub_name = $subDetails['sub_name'];
        $sub_stdcount = $subDetails['sub_stdcount'];
        $sub_day = $subDetails['sub_day'];
        $sub_start = $subDetails['sub_start'];
        $sub_end = $subDetails['sub_end'];
        $sub_hall = $subDetails['sub_hall'];
        $sub_available = $subDetails['sub_available'];
        
        $dbo = Database::getDBOInstance();
        //Update sase_subject table
        $sql = "update sase_subject set sub_name = '$sub_name', sub_day = '$sub_day', sub_start = '$sub_start', " 
            . "sub_end = '$sub_end', sub_hall = '$sub_hall', sub_available = $sub_available where sub_code = '$subCode'";

        $dbo->mysql_query($sql);
        
        //Check if ds change
        $list_defaults = Subject::getDefaultSupervisors();
        
        foreach($sub_default_supervisors as $new_sup) {
            if ( array_search($new_sup, $list_defaults) === FALSE ) {
                //This is a new one
                Supervisor::enrolDefaultSupervisor($subCode, $new_sup);
            }
        }
        
        //Remove for DSs who are not in the new list
        
        foreach ($list_defaults as $old_sup) {
            if ( array_search($old_sup, $sub_default_supervisors) == FALSE ) {
                Supervisor::dropSubject($old_sup, $subCode);
            }
        }
        
    }
    
    public static function getDefaultSupervisors($subCode) {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from default_supervisor where sub_code = '$subCode'");
        
        $list = array();
        while ($row = mysql_fetch_assoc($result)) {
            $list[] = $row['sup_id'];
        }
        
        return $list;
    }
    
    public static function getSubjectDetails($subCode) {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from sase_subject where sub_code = '$subCode'");
        
        $row = mysql_fetch_assoc($result);
        $detals = array('sub_code' => $row['sub_code'], 'sub_name' => $row['sub_name'], 'sub_stdcount' => $row['sub_stdcount'], 
            'sub_day' => $row['sub_day'], 'sub_start' => $row['sub_start'], 'sub_end' => $row['sub_end'], 
            'sub_available' => $row['sub_available'], 'sub_hall' => $row['sub_hall']);
        
        return $detals;
    }
}
?>